package com.example.myapplication22;

import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.util.Log;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.util.Timer;
import java.util.TimerTask;

public class MqttManager {
      void send(String broker, String topic, String content){
        int qos = 1; //保证消息接收者至少会收到一次，可能造成消息重复
        String userName = "test";
        String password = "test";
        String clientId = "pubClient";
        try {
            MqttClient sampleClient = new MqttClient(broker, clientId, new MemoryPersistence());  // 创建客户端
            MqttConnectOptions connOpts = new MqttConnectOptions(); // 创建链接参数
            connOpts.setCleanSession(false); // 在重新启动和重新连接时记住状态
            connOpts.setUserName(userName);  // 设置连接的用户名
            connOpts.setPassword(password.toCharArray());
            sampleClient.connect(connOpts); // 建立连接
            MqttMessage message = new MqttMessage(content.getBytes());  // 创建消息
            message.setQos(qos);   // 设置消息的服务质量
            sampleClient.publish(topic, message);  // 发布消息
            sampleClient.disconnect(); // 断开连接
            sampleClient.close(); // 关闭客户端
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
            me.printStackTrace();
        }
    }

       String subscribe(String HOST, String TOPIC){
        int qos = 1; //保证消息接收者至少会收到一次，可能造成消息重复
        String clientid = "subClient3";
        String userName = "test";
        String passWord = "test";
        String a=null;
        try {
            // host为主机名，test为clientid即连接MQTT的客户端ID，一般以客户端唯一标识符表示，MemoryPersistence设置clientid的保存形式，默认为以内存保存
            MqttClient client = new MqttClient(HOST, clientid, new MemoryPersistence());
            MqttConnectOptions options = new MqttConnectOptions(); // MQTT的连接设置
            // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录，这里设置为true表示每次连接到服务器都以新的身份连接
            options.setCleanSession(true);
            options.setUserName(userName);  // 设置连接的用户名
            options.setPassword(passWord.toCharArray()); // 设置连接的密码
            options.setConnectionTimeout(10);  // 设置超时时间 单位为秒
            // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线，但这个方法并没有重连的机制
            options.setKeepAliveInterval(20);
            client.setCallback(new MqttCallback() { // 设置回调函数
                public void connectionLost(Throwable cause) {
                    System.out.println("connectionLost");
                    cause.printStackTrace();
                }

                public void messageArrived(String topic, MqttMessage message) {
                    String content = new String(message.getPayload());
                    a.equals(content);
                }
                public void deliveryComplete(IMqttDeliveryToken token) {
                    System.out.println("deliveryComplete---------"+ token.isComplete());
                }
            });
            client.connect(options);
            client.subscribe(TOPIC, qos);  //订阅消息
        } catch (Exception e) {
            e.printStackTrace();
        }
        return a;
    }
}
